from django.db import models

# Create your models here.

# 资源表
class ResourceModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="ID")
    name = models.CharField(max_length=50, null=True, blank=True, verbose_name="资源名称")
    pid = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='ResourceID', verbose_name="父级ID")
    url = models.CharField(max_length=100, null=True, blank=True, verbose_name="资源url")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'resource'
        verbose_name_plural = '资源'

# 部门模型
class Department(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, unique=True, null=True, blank=True, verbose_name='部门名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'department'
        verbose_name_plural = '部门'

# 职位模型
class Position(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, unique=True, null=True, blank=True, verbose_name='职位名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'position'
        verbose_name_plural = '职位'

# 角色表
class RoleModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="ID")
    name = models.CharField(max_length=50, unique=True, verbose_name="角色名称")
    pid = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='RoleID', verbose_name="父级ID")
    type = models.CharField(max_length=50, null=True, blank=True, verbose_name="类型")
    resources = models.ManyToManyField(ResourceModel, verbose_name="角色资源")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'role'
        verbose_name_plural = '角色'

# 用户表
class AdminUserModel(models.Model):
    phone = models.CharField(max_length=11, unique=True, verbose_name="手机号")
    password = models.CharField(max_length=32, null=True, blank=True, verbose_name="密码")
    id = models.AutoField(primary_key=True, verbose_name="用户id")
    name = models.CharField(max_length=50, unique=True, verbose_name="用户姓名")
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name="用户角色")
    dept_id = models.ForeignKey(Department, on_delete=models.CASCADE, null=True, blank=True, verbose_name='部门')
    position_id = models.ForeignKey(Position, on_delete=models.CASCADE, null=True, blank=True, verbose_name='职位')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'user'
        verbose_name_plural = '用户'

# 接口地址表
class ResourceInterface(models.Model):
    name = models.CharField(max_length=50, null=True, blank=True, verbose_name="接口名称")
    url = models.CharField(max_length=100, null=True, blank=True, verbose_name="接口url")
    resid = models.ManyToManyField(ResourceModel, null=True, blank=True, related_name='resource_interface', verbose_name="关联资源")

    def __str__(self):
        return self.url

    class Meta:
        verbose_name = "resource_interface"
        verbose_name_plural = '接口权限表'

# 互斥表
class MutexModel(models.Model):
    resource1 = models.ForeignKey(ResourceModel, on_delete=models.CASCADE, null=True, blank=True, related_name='mutex_resource1', verbose_name="资源1")
    resource2 = models.ForeignKey(ResourceModel, on_delete=models.CASCADE, null=True, blank=True, related_name='mutex_resource2', verbose_name="资源2")

    def __str__(self):
        # 假设您想在字符串表示中显示两个资源的名称
        return f"{self.resource1.name} - {self.resource2.name}" if self.resource1 and self.resource2 else ""

    class Meta:
        verbose_name = 'mutex'
        verbose_name_plural = '互斥表'
